package com.jd.org.controller;


import com.jd.basic.annotation.PreAuthorize;
import com.jd.basic.jwt.UserInfo;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Employee;
import com.jd.org.query.EmployeeQuery;
import com.jd.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


//    /**
//     * Restful 风格 路径变量
//     * http://localhost:80/dept/1
//     * 根据id查询一个
//     * dId
//     */
//    @GetMapping("/{id}")
//    @ApiOperation(value = "通过ID查询详情")
//    public Employee findById(@ApiParam(value = "部门id", required = true) @PathVariable("id") Long id) {
//        return employeeService.findById(id);
//
//    }
//
//
//    /**
//     * 添加或者修改
//     *
//     * @param employee
//     * @return
//     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        System.err.println("进入Controller" + employee);
        if (null != employee.getId()) {
            // 修改
            employeeService.update(employee);
        } else {
            // 添加
            employeeService.add(employee);
        }
        return AjaxResult.me().success();
    }
//
    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除员工",sn = "/emp:/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
        UserInfo userInfo = LoginContext.getUserInfo();

        if (null != userInfo.getShop()) {
            query.setShopId(userInfo.getShop().getId());
        }
        System.err.println("分页查询" + query);
        System.err.println("分页查询结果" + employeeService.queryByPage(query));
        return employeeService.queryByPage(query);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public List<Employee> findAll() {
        System.err.println("Controller层" + employeeService.findAll() );
        return employeeService.findAll();
    }

    /**
     * 批量删除
     *
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        System.err.println("批量删除" + ids);
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/{id}")
    public List<Employee> findEmpByDeptId(@PathVariable Long id){
        System.err.println("进入Controller" + id);
        return employeeService.findEmpByDeptId(id);
    }
}
